/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import domain.Task;
import domain.Userdom;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Ale
 */
public class TaskDao {

    EntityManager em;
    EntityTransaction tx;

    public TaskDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Task getTask(Integer idTask) {
        Task n = (Task) em.createNativeQuery("select * from task as t where t.idTask='" + idTask.intValue() + "'", Task.class).getSingleResult();
        return n;
    }

    public void saveTask(Task t) {
        tx.begin();
        em.persist(t);
        tx.commit();
    }

    public void removeTask(Task t) {
        tx.begin();
        em.remove(t);
        tx.commit();
    }

    public Set<Task> getTaskList(Userdom user) {

        Set<Task> s = new HashSet(em.createNativeQuery("select * from task as t where t.userFK='" + user.getNick() + "'", Task.class).getResultList());
        return s;
    }
}
